<?php
/**
*
*for in Progress 1 = not started waiting for players, 
*                2 = started waiting for players,
*                3 = started no more players may join 
*                4 = finished.
*/
   function connectToDB()
   {
      $link = mysql_connect('localhost:3306', 'dactry', 'cool', null, MYSQL_CLIENT_SSL);

      if (!$link) {
         print "Could not connect: " . mysql_error() . "<br>";
         return null;
      }

      if (!mysql_select_db('gameserver', $link)) {
         print "Could not select database <br>";
         return null;
      }

      //echo 'Connected successfully';
      //mysql_close($link);
      return $link;
   }
  
   function authenticate($username, $password)
   {
		$link = connectToDB(); 
		$sql    = "SELECT playerName,playerPassword FROM player WHERE playerName='$username'";
		$result = mysql_query($sql, $link);

		if (!$result) 
		{
			print "DB Error, could not query the database\n";
			print "MySQL Error: " . mysql_error();

			return 0;
		}

		$row = mysql_fetch_array($result, MYSQL_NUM);
 
		$hashpw = md5($password); 

		//print "$username $password\n";
		//print "$hashpw \n" . md5($password); 

		if ($hashpw == $row[1])
			return 1;

		return 0; 

   }

function getGamesList($game)
{
      $link = connectToDB(); 
      $ret = "";
      $sql    = "SELECT * FROM gameTable WHERE status <> 4 and gameName = '" . $game . "'";
      $result = mysql_query($sql, $link);

      if (!$result) 
      {
         print "DB Error, could not query the database\n";
         print "MySQL Error: " . mysql_error();

         return 0;
      }
      $count = 0;
      while ($row = mysql_fetch_row($result))
      {
          $ret .= "@";
          foreach($row as $col)
          {
             $ret .= $col . ';';
          }
         /*if($count++&1)
             $ret .=  "<tr class='oddrow'>\n";
         else
             $ret .=  "<tr class= 'evenrow'>\n";
         for($i=0; $i < count($row); $i++)
         {
            if($i == 2)
            { 
                switch($row[$i])
                {
                    case 1:
                        $ret .=  "<td>Waiting</td>\n"; 
                        break;
                    case 2:
                        $ret .= "<td>Started:open</td>\n"; 
                        break;
                    case 3:
                        $ret .= "<td>Started:closed</td>\n"; 
                        break;

                }
            }
            else
            {
                $ret .= "<td>" . $row[$i] . "</td>\n";
            }
       
         }   
         $ret .= "</tr>\n";*/
      }
      return $ret;
}


?>


  


